package com.example.cadmin.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.cadmin.entity.BorrowingRecords;
import com.example.cadmin.service.BorrowingRecordsService;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 借阅记录 前端控制器
 * </p>
 *
 * @author zzsoft
 * @since 2023-02-20
 */
@Controller
@RequestMapping("/borrowing/records")
public class BorrowingRecordsController {
    @Resource
    public BorrowingRecordsService borrowingRecordsService;

    @GetMapping("/index")
    public String index() {
        return "BorrowingRecords/records-list";
    }

    /**
     * 借阅记录-借出档案-表格接口
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/takeOut/list")
    @ResponseBody
    public Map<String,Object> takeOutRecordsList(@RequestParam(value = "fileName",required = false) String fileName,
                                       @RequestParam(value = "page") int page,
                                       @RequestParam(value = "limit") int limit, HttpServletRequest request){
        return borrowingRecordsService.recordsList(fileName,page,limit,1,request);
    }

    /**
     * 借阅记录-归还档案-表格接口
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/return/list")
    @ResponseBody
    public Map<String,Object> returnRecordsList(@RequestParam(value = "fileName",required = false) String fileName,
                                          @RequestParam(value = "page") int page,
                                          @RequestParam(value = "limit") int limit, HttpServletRequest request){
        return borrowingRecordsService.recordsList(fileName,page,limit,2,request);
    }

    /**
     * 清空日志
     */
    @PostMapping("clear/log")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('borrowinglog:clear')")
    public ResponseModel clearRecords() {
        UpdateWrapper<BorrowingRecords> fileLogUpdateWrapper = new UpdateWrapper<>();
        fileLogUpdateWrapper.set("status", 0);
        borrowingRecordsService.update(null, fileLogUpdateWrapper);
        return ResponseModel.success("日志已清空");
    }

    /**
     * 借阅审批
     */
    @GetMapping("/approval/index")
    @PreAuthorize("hasAnyAuthority('approval:list')")
    public String borrowingApproval(){
        return "Approval/approval-list";
    }

    /**
     * 借阅审批list接口
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/approval/list")
    @ResponseBody
    public Map<String,Object> approvalList(@RequestParam(value = "fileName",required = false) String fileName,
                                           @RequestParam(value = "userName",required = false) String userName,
                                                 @RequestParam(value = "page") int page,
                                                 @RequestParam(value = "limit") int limit, HttpServletRequest request){
        return borrowingRecordsService.approvalList(fileName,userName,page,limit,request);
    }

    /**
     * 审批状态保存
     */
    @PostMapping("/approval/save")
    @ResponseBody
    public ResponseModel approvalSave(@RequestBody JSONObject data) {

        return borrowingRecordsService.approvalSave(data);
    }
}
